package com.hq.door.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hq.door.dao.TOutRequestDAO;
import com.hq.door.dao.TStudentDAO;
import com.hq.door.dto.OutRequest;
import com.hq.door.dto.OutrequestInfo;
import com.hq.door.dto.ParentInfoBack;
import com.hq.door.dto.TRecordInfo;
import com.hq.door.dto.Teacher;
import com.hq.door.dto.TeacherDto;
import com.hq.door.pojo.TOutRequest;
import com.hq.door.pojo.TStudent;
import com.hq.door.pojo.TTeacher;
import com.hq.door.service.TOutRequestService;
import com.hq.door.util.POIUtil;

@Service("tOutRequestService")
public class TOutRequestServiceImpl extends BaseServiceImpl<TOutRequest>
		implements TOutRequestService {
	@Autowired
	TOutRequestDAO tOutRequestDAO;
	
	@Autowired
	TStudentDAO tStudentDAO ;

	public TOutRequestDAO gettOutRequestDAO() {
		return tOutRequestDAO;
	}

	@Autowired
	public void settOutRequestDAO(TOutRequestDAO tOutRequestDAO) {
		this.setBaseDao(tOutRequestDAO);
		this.tOutRequestDAO = tOutRequestDAO;
	}

	@Override
	public List<OutrequestInfo> showAll(int pageStart, int pageSize) {
		List<OutrequestInfo> outrequestInfos = new ArrayList<OutrequestInfo>();
		List<TOutRequest> tOutRequests = new ArrayList<TOutRequest>();
		pageStart = (pageStart - 1) * pageSize;

		String hql = "from TOutRequest";
		tOutRequests = tOutRequestDAO.findPageObjects(hql, pageStart, pageSize);
		outrequestInfos = new OutrequestInfo().getTOutrequest(tOutRequests);

		return outrequestInfos;

	}

	@Override
	public int getCount() {
		String hql = "select count(t.id) from TOutRequest t";
		int count = tOutRequestDAO.getCount(hql);
		return count;
	}
	/**
	 * 修改审批状态为撤销
	 */
	public String modifyOutrequest(OutrequestInfo pib){
		//1.根据ID获取tOutRequest 对象
		TOutRequest tOutRequest = tOutRequestDAO.get(pib.getId());
		//2.设置status=3
		tOutRequest.setAuthStatus(3);
		//3.update tOutRequest 对象
		tOutRequestDAO.update(tOutRequest);
		return "撤销成功！";
	}
	
	@Override
	public void excelExport(List<OutrequestInfo> infos,
			HttpServletResponse response) {
		String title = "刷卡记录";
		String[] colName = { "编号", "专业", "班级", "学生姓名", "申请出发时间", "申请回校时间",
				"班主任", "请假缘由", "审批状态" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		Object[] objs = null;
		for (OutrequestInfo outrequest : infos) {
			objs = new Object[colName.length];
			objs[0] = outrequest.getId();
			if (outrequest.getMajorname() != null) {
				objs[1] = outrequest.getMajorname();
			} else {
				objs[1] = "未知专业";
			}
			if (outrequest.getClassname() != null) {
				objs[2] = outrequest.getClassname();
			} else {
				objs[2] = "未知班级";
			}
			if (outrequest.getStudentname() != null) {
				objs[3] = outrequest.getStudentname();
			} else {
				objs[3] = "未知学生";
			}
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (outrequest.getStartTime() != null) {
				objs[4] = df.format(outrequest.getStartTime());
			} else {
				objs[4] = "未知时间";
			}
			if (outrequest.getEndTime() != null) {
				objs[5] = df.format(outrequest.getEndTime());
			} else {
				objs[5] = "未知时间";
			}
			if (outrequest.getTeachername() != null) {
				objs[6] = outrequest.getTeachername();
			} else {
				objs[6] = "未知老师";
			}

			if (outrequest.getRequestReason() != null) {
				objs[7] = outrequest.getRequestReason();
			} else {
				objs[7] = "未知原因";
			}

			if (outrequest.getAuthStatus() != null) {
				switch (outrequest.getAuthStatus()) {
				case 0:
					objs[8] =  "审批中";
					break;
				case 1:
					objs[8] =  "审批通过";
					break;
				case 2:
					objs[8] =  "审批驳回";
					break;
				case 3:
					objs[8] =  "审批撤销";
					break;
				default:
					objs[8] = "未知状态";
					break;
				}
			}else{
				objs[8] = "未知状态";
			}
			
			dataList.add(objs);
		}
		POIUtil poiUtil = new POIUtil(title, colName, dataList);
		try {
			poiUtil.export(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
